package com.imageim.yimirror.common.ui.btn;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.imageim.yimirror.common.util.UIUtils;

/**
 * Created by Bruce.Zhou on 2018/8/6 11:26.
 * Email: 907160968@qq.com
 */

public class BtnScaleFixedWidth extends SurfaceView implements Runnable {

    private Paint paint;
    private static final int STATE_UNSELECTED = 0;
    private static final int STATE_SELECTED = 1;
    private int state = STATE_UNSELECTED;
    private SurfaceHolder holder;
    private boolean isDrawing;
    private String textColor1 = "色彩报告";
    private String textColor1_en = "Color Report";
    private String textColor2 = "色彩";
    private String textColor2_en = "Color";
    private String textStyle1 = "廓形";
    private String textStyle1_en = "Silhouette";
    private String textStyle2 = "廓形报告";
    private String textStyle2_en = "Silhouette";
    private static final int fontSize = 26; // 字体大小
    private static final int BORDER_WIDTH = 3; // 边框宽度
    private float downX;
    private float smallMaxWidth;
    private float bigMaxWidth;
    private float gapWidth; // 间隙
    private int selectIndex = 0; // 0表示第一个按钮，1表示第二个
    private float btn1Right;
    private float btn2Left;
    private static final int MOVE_SPEED = 10;
    private boolean touchable = true;

    public interface OnSelectListener {
        void onSelect(int index);
    }

    private OnSelectListener selectListener;

    public BtnScaleFixedWidth(Context context) {
        super(context);
        init();
    }

    public void setOnSelectListener(OnSelectListener selectListener) {
        this.selectListener = selectListener;
    }

    public BtnScaleFixedWidth(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BtnScaleFixedWidth(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(BORDER_WIDTH);
        paint.setAntiAlias(true);
        paint.setTextSize(fontSize);
        paint.setStrokeJoin(Paint.Join.ROUND);
        //        mPath = new Path();
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (!touchable) {
                            return true;
                        }
                        downX = motionEvent.getX();
                        //                        downY = motionEvent.getY();
                        if (downX < btn1Right) {
                            selectIndex = 0;
                        } else {
                            selectIndex = 1;
                        }
                        //                        toggleState();
                        if (selectListener != null) {
                            selectListener.onSelect(selectIndex);
                        }
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        //        setZOrderOnTop(true);
        holder = getHolder();
        holder.setFormat(PixelFormat.TRANSLUCENT);
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if (isDrawing == false) {
                    isDrawing = true;
                    new Thread(BtnScaleFixedWidth.this).start();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                isDrawing = false;
            }
        });
    } // init

    public void setTouchable(boolean touchable) {
        this.touchable = touchable;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    private boolean isInit = false;

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (!isInit) {
            gapWidth = BORDER_WIDTH * 2;
            bigMaxWidth = (getWidth() - gapWidth) * 3f / 5;
            smallMaxWidth = (getWidth() - gapWidth) * 2f / 5;
            btn1Right = bigMaxWidth;
            btn2Left = bigMaxWidth + gapWidth;
            isInit = true;
        }
    }

    @Override
    public void run() {
        while (isDrawing) {
            draw();
            try {
                Thread.sleep(8);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void toggleState() {
        if (selectIndex == 0) {
            selectIndex = 1;
        } else {
            selectIndex = 0;
        }
    }


    private void draw() {
        Canvas canvas = holder.lockCanvas();
        if (canvas == null) {
            return;
        }
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        if (selectIndex == 0) {
            canvas.drawRect(gapWidth, gapWidth, btn1Right - gapWidth, getHeight() - gapWidth, paint);
            canvas.drawRect(btn2Left, gapWidth, getWidth() - gapWidth, getHeight() - gapWidth, paint);
            if (btn1Right < bigMaxWidth) {
                btn1Right += MOVE_SPEED;
            } else {
                btn1Right = bigMaxWidth;
            }
            if (btn2Left < bigMaxWidth + gapWidth) {
                btn2Left += MOVE_SPEED;
            } else {
                btn2Left = bigMaxWidth + gapWidth;
            }
            // text
            paint.setStyle(Paint.Style.FILL);
            canvas.drawText(textColor1, (btn1Right - UIUtils.getTextWidth(textColor1, paint)) / 2
                    , (getHeight() - UIUtils.getTextHeight(textColor1, paint)) / 2, paint);
            float text2Width = UIUtils.getTextWidth(textStyle1, paint);
            canvas.drawText(textStyle1, btn2Left + (getWidth() - gapWidth - btn2Left - text2Width) / 2
                    , (getHeight() - UIUtils.getTextHeight(textStyle1, paint)) / 2, paint);
            // en
            canvas.drawText(textColor1_en, (btn1Right - UIUtils.getTextWidth(textColor1_en, paint)) / 2
                    , (getHeight() - UIUtils.getTextHeight(textColor1_en, paint)) / 2
                            + UIUtils.getTextHeight(textColor1_en, paint) * 1.6f, paint);
            text2Width = UIUtils.getTextWidth(textStyle1_en, paint);
            canvas.drawText(textStyle1_en, btn2Left + (getWidth() - gapWidth - btn2Left - text2Width) / 2
                    , (getHeight() - UIUtils.getTextHeight(textStyle1_en, paint)) / 2
                            + UIUtils.getTextHeight(textStyle1_en, paint) * 1.6f, paint);
            paint.setStyle(Paint.Style.STROKE);
        } else if (selectIndex == 1) {
            canvas.drawRect(gapWidth, gapWidth, btn1Right - gapWidth, getHeight() - gapWidth, paint);
            canvas.drawRect(btn2Left, gapWidth, getWidth() - gapWidth, getHeight() - gapWidth, paint);
            if (btn1Right > smallMaxWidth) {
                btn1Right -= MOVE_SPEED;
            } else {
                btn1Right = smallMaxWidth;
            }
            if (btn2Left > smallMaxWidth + gapWidth) {
                btn2Left -= MOVE_SPEED;
            } else {
                btn2Left = smallMaxWidth + gapWidth;
            }

            // text
            paint.setStyle(Paint.Style.FILL);
            canvas.drawText(textColor2, (btn1Right - UIUtils.getTextWidth(textColor2, paint)) / 2
                    , (getHeight() - UIUtils.getTextHeight(textColor2, paint)) / 2, paint);
            float text2Width = UIUtils.getTextWidth(textStyle2, paint);
            canvas.drawText(textStyle2, btn2Left + (getWidth() - gapWidth - btn2Left - text2Width) / 2
                    , (getHeight() - UIUtils.getTextHeight(textStyle2, paint)) / 2, paint);

            // en
            canvas.drawText(textColor2_en, (btn1Right - UIUtils.getTextWidth(textColor2_en, paint)) / 2
                    , (getHeight() - UIUtils.getTextHeight(textColor2_en, paint)) / 2
                            + UIUtils.getTextHeight(textColor2_en, paint) * 1.6f, paint);
            text2Width = UIUtils.getTextWidth(textStyle2_en, paint);
            canvas.drawText(textStyle2_en, btn2Left + (getWidth() - gapWidth - btn2Left - text2Width) / 2
                    , (getHeight() - UIUtils.getTextHeight(textStyle2_en, paint)) / 2
                            + UIUtils.getTextHeight(textStyle2_en, paint) * 1.6f, paint);
            paint.setStyle(Paint.Style.STROKE);
        }
        if (holder != null) {
            try {
                holder.unlockCanvasAndPost(canvas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isDrawing = false;
    }


    public void setSelectIndex(int index) {
        selectIndex = index;
    }
}
